package com.welldone.service;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;

import com.welldone.po.User;

/**
 *  用户服务类
 *@Description
 *@author Rain
 *@createDate 2010-05-25
 *@version V1.0
 */
public class LoginService {

	/**
	 * 用户登录
	 *@author Rain
	 *@createDate 2011-5-25
	 *@param uName
	 *@param uPass
	 *@return User
	 */
	public User userLogin(String uName, String uPass) {
		String sql = "select * from t_user where userName=? and password = ? and useable != ?";
		String fuName = uName;
		String fuPass = uPass;
		Object[] objs  = {fuName,fuPass,"1"};
		Object obj = DBService.getJdbcTemplate().query(sql,objs,new ResultSetExtractor() {
			public Object extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				User user = new User();
				while (rs.next()) {
						user.setUserId(rs.getString("userId"));
						user.setUserName(rs.getString("userName"));
						user.setUserPass(rs.getString("password"));
						user.setUserStatus(rs.getString("loginstate"));
						user.setUserAble(rs.getString("useable"));
				}
				return user;
			}
		});
		if(null == obj) return null;
		User user = (User)obj;
		return user;
	}
	
	public void resetUserStatus(){
		String sql="update t_user set loginstate = ?";
		Object[] objs  = {"0"};
		int result = DBService.getJdbcTemplate().update(sql, objs);
		if(result>0){
			System.out.println("初始化用户状态成功");
		}else{
			System.out.println("初始化用户状态失败");
		}
	}
	
}
